home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / toolssrc / Mosmldep.mlp < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.6 KB  |  106 lines  |  [TEXT/R*ch]

  1. (* Mosmldep -- computing dependencies in a Moscow ML source directory.
  2.    Handles strings and nested comments correctly; normalizes file names
  3.    under DOS.
  4.  
  5.    Usage: mosmldep
  6. *)
  7.  
  8. #ifdef unix
  9. fun manglefilename s = s
  10. #endif
  11.  
  12. #ifdef msdos
  13. fun manglefilename s = 
  14.     CharVector.tabulate(Int.min(8, size s), 
  15.             fn i => Char.toLower(String.sub(s, i)))
  16. #endif
  17.  
  18. open BasicIO List
  19.  
  20. (* Lexer of stream *)
  21.  
  22. fun createLexerStream (is : instream) =
  23.   Lexing.createLexer (fn buff => fn n => Nonstdio.buff_input is buff 0 n)
  24. ;
  25.  
  26. fun parsePhraseAndClear parsingFun lexingFun lexbuf =
  27.   let val phr =
  28.     parsingFun lexingFun lexbuf
  29.     handle x => (Parsing.clearParser(); raise x)
  30.   in
  31.     Parsing.clearParser();
  32.     phr
  33.   end;
  34.  
  35. val parseFile =
  36.   parsePhraseAndClear Deppars.MLtext Deplex.Token;
  37.  
  38. fun addExt s ext = s ^ "." ^ ext
  39.  
  40. local 
  41.     fun say s = (output(std_out, s); flush_out std_out)
  42.     val col = ref 0
  43.     and res = ref [];
  44.  
  45.     fun outstring s =
  46.     if !col + size s >= 76 then
  47.         (print ("\\\n    " ^ s ^ " ");
  48.          col := 5 + size s)
  49.     else
  50.         (print (s ^ " ");
  51.          col := !col + size s + 1);
  52. in
  53.     fun outname s =
  54.     if FileSys.access (addExt s "sig", []) then
  55.         res := addExt s "ui" :: !res
  56.     else if FileSys.access (addExt s "sml", []) then
  57.         res := addExt s "uo" :: !res
  58.         else ();
  59.  
  60.     fun beginentry objext target =
  61.     let val targetname = addExt target objext
  62.     in 
  63.         res := [targetname ^ ":"];
  64.         if objext = "uo" andalso FileSys.access(addExt target "sig", [])
  65.         then res := addExt target "ui" :: !res else ()
  66.     end;
  67.  
  68.     fun endentry () =
  69.     if length(!res) > 1 then
  70.         (col := 0; 
  71.          app outstring (rev (!res));
  72.          print "\n")
  73.     else ();     
  74. end;
  75.  
  76. fun read srcext objext filename =
  77.     let val is       = open_in (addExt filename srcext)
  78.     val lexbuf   = createLexerStream is
  79.     val mentions = Hasht.new 37 : (string, unit) Hasht.t
  80.     val names    = parseFile lexbuf 
  81.     in 
  82.     beginentry objext (manglefilename filename);
  83.     app (fn name => Hasht.insert mentions name ()) names;
  84.     Hasht.apply (fn name => fn _ => outname (manglefilename name))
  85.                 mentions;
  86.     close_in is;
  87.     endentry ()
  88.     end
  89.     handle Parsing.ParseError _ => output(std_out, "Parseerror!\n");
  90.  
  91. fun processfile filename =
  92.     let (* val _ = output(std_err, "Processing " ^ filename ^ "\n"); *)
  93.     val {base, ext} = Path.splitBaseExt filename
  94.     in 
  95.     case ext of
  96.         SOME "sig" => read "sig" "ui" base
  97.       | SOME "sml" => read "sml" "uo" base
  98.       | _          => ()
  99.     end
  100.  
  101. fun main () =
  102.     (List.app processfile (Mosml.listDir "."))
  103.     handle OS.SysErr (str, _) => output(std_err, str ^ "\n\n")
  104.  
  105. val _ = main ();
  106.